package com.tianpengtech.common.model.task;

import com.tianpengtech.common.model.AbstractBaseModel;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.util.ToolDateTime;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 实体类名-任务表 fair_task
 *
 * @since Fri May 26 13:29:02 HKT 2017
 */
public class Task extends AbstractBaseModel<Task> {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public static final Task dao = new Task();
	public static String TASK_CACHE_NAME = "task";
	public static String TASK_CACHE_CLICK_NUM_KEY = "task_clicks_";
	public static String TASK_CACHE_RECE_NUM_KEY = "task_reces_";
	public static String TASK_CACHE_CVS_NUM_KEY = "task_cvss_";
	public static String TASK_CACHE_MAIN_FAIR_TOP_KEY = "task_main_top_";

	public static final String ID = "id"; //
	public static final String TASK_TITLE = "task_title"; // 任务标题
	public static final String TASK_START_TIME = "task_start_time"; // 任务开始时间
	public static final String TASK_END_TIME = "task_end_time"; // 任务结束时间
	public static final String TASK_PRICE = "task_price"; // 任务酬劳
	public static final String TASK_DESCRIPTION = "task_description"; // 任务说明
	public static final String STATUS = "status"; // 任务状态
	public static final String TASK_URL = "task_url"; // 任务链接
	public static final String CREATE_USER = "create_user"; // 创建者
	public static final String CREATE_TIME = "create_time"; // 创建时间
	public static final String DELETETAG = "deletetag"; // 删除标识
	public static final String EXTENSION_TYPE = "extension_type"; // 推广方式
	public static final String WH_NUMBER = "wh_number"; // 主播人数
	public static final String TASK_TYPE = "task_type"; // 任务类型
	public static final String SUCCESS = "success"; // 是否完成
	public static final String WEB_URL = "web_url"; // 推广链接
	public static final String H5_TYPE = "h5_type"; // 是否有H5页面


	//白名单表 相关字段
	public static final String TASK_WHITELIST ="fair_task_whitelist";//表名
	public static final String TASK_WHITELIST_CACHE_NAME ="whitelist";//缓存名称
	public static final String WL_TASK_ID ="task_id";//任务ID
	public static final String WL_FAIR_ID ="fair_id";//网红ID


	public static final int SUCCESS_NO=0;
	public static final int SUCCESS_YES=1;

	
	public static final int STATUS_APPROVING=1;
	public static final int STATUS_SUCCESS=2;
	public static final int STATUS_FAILURE=3;
	public static final int STATUS_WAIT=4;
	public static final int STATUS_START=5;
	public static final int STATUS_COMPLETED=6; //已完成

	public static final int DELETE_STATUS_YES=1;
	public static final int DELETE_STATUS_NO=0;


	public static final int EXT_TYPE_ZD = 1; //指定主播
	public static final int EXT_TYPE_RL = 2; //任务认领

	public static final int TASK_TYPE_ONLINE = 1; //线上
	public static final int TASK_TYPE_OFLINE = 2; //线下

	public static final int H5_TYPE_NO = 1; //没有H5页面
	public static final int H5_TYPE_YES = 2; //有H5页面
	
	
	public String type(){
		String result="";
		if(getInt(EXTENSION_TYPE)==EXT_TYPE_ZD){
			result = "指定主播";
		}else if(getInt(EXTENSION_TYPE)==EXT_TYPE_RL){
			result = "指定主播";
		}else{
			result = "未知状态";
		}
		return result;
	}
	
	public String status(){
		String result="";
		if(getInt(STATUS)==STATUS_APPROVING){
			result="审核中";
		}else if(getInt(STATUS)==STATUS_SUCCESS){
			if (isStarted()){
				result="进行中";
			}else{
				result="未开始";
			}
		}else if(getInt(STATUS)==STATUS_FAILURE){
			result="审核失败";
		}else if(getInt(STATUS)==STATUS_WAIT){
			result="草稿";
		}else if(getInt(STATUS)==STATUS_COMPLETED){
			result="已完成";
		}else{
			result="未知状态";
		}
		return result;
	}
	
	public String h5Type(){
		String result="";
		if(getInt(H5_TYPE)==H5_TYPE_YES){
			result="有H5页面";
		}else if(getInt(H5_TYPE)==H5_TYPE_NO){
			result="无H5页面";
		}
		return result;
	}

	/**
	 * 任务是否开始
	 * @return
	 */
	public boolean isStarted(){
//		return ToolDateTime.getDateDaySpace(getDate(TASK_START_TIME),new Date())>=0;
		return ToolDateTime.getDateMinuteSpace(getDate(TASK_START_TIME),new Date())>=0;
	}

	/**
	 * 获取点击量
	 * @return
	 */
	public long getClick(){
		Task task=dao.findFirstByCache(TASK_CACHE_NAME,TASK_CACHE_CLICK_NUM_KEY+get(ID),"select sum("+TaskWh.CLICK_NUM+") as num from "+ TaskWh.dao.tableName()+" where "+TaskWh.TASK_ID +"=?",new Object[]{get(ID)});
		BigDecimal num=task.getBigDecimal("num");
		if (num==null){
			return 0;
		}
		return num.longValue();
	}

	/**
	 * 获取转化量
	 * @return
	 */
	public long getCvs(){
		Task task=dao.findFirstByCache(TASK_CACHE_NAME,TASK_CACHE_CVS_NUM_KEY+get(ID),"select sum("+TaskWh.CVS_NUM+") as num from "+ TaskWh.dao.tableName()+" where "+TaskWh.TASK_ID +"=?",new Object[]{get(ID)});
		BigDecimal num=task.getBigDecimal("num");
		if (num==null){
			return 0;
		}
		return num.longValue();
	}

	/**
	 * 获取接单人数
	 * @return
	 */
	public long getRece(){
		Task task=dao.findFirstByCache(TASK_CACHE_NAME,TASK_CACHE_RECE_NUM_KEY+get(ID),"select count("+TaskWh.ID+") as num from "+ TaskWh.dao.tableName()+" where "+TaskWh.TASK_ID +"=?",new Object[]{get(ID)});
		Long num=task.getLong("num");
		if (num==null){
			return 0;
		}
		return num;
	}

	public String web_url(){
		String webUrl = getStr(WEB_URL);
		if(webUrl==null){
			webUrl = "";
		}
		return (webUrl.startsWith("http")?webUrl:"http://"+webUrl);
	}

	/**
	 * 获取点击量前三的网红信息
	 * @return
	 */
	public List<Fair> getFairTop(){
		String sql = "select fair.*,SUM(tw."+TaskWh.CLICK_NUM+") cnum from "+TaskWh.dao.tableName()+" tw left join "+Fair.dao.tableName()+" fair on fair."+Fair.ID+"=tw."+TaskWh.WH_ID+" where "+TaskWh.TASK_ID+ "=? and tw."+TaskWh.DELETETAG+"="+TaskWh.DELETE_STATUS_NO+" GROUP BY "+TaskWh.WH_ID+" ORDER by SUM(tw."+TaskWh.CLICK_NUM+") desc LIMIT 3";
		return Fair.dao.findByCache(Fair.FAIR_CACHE_NAME,TASK_CACHE_MAIN_FAIR_TOP_KEY+get(ID),sql,new Object[]{get(ID)});
	}

	/**
	 * 获取开始时间到结束时间的字符串
	 * @return
	 */
	public String getDateString(){
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY.MM.dd");
		return sdf.format(getDate("task_start_time"))+"--"+sdf.format(getDate("task_end_time"));
	}
	
	@Override
	public String tableName() {
		return "fair_task";
	}



}